{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 导包@常量准备"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "import numpy as np\n",
    "from functools import reduce\n",
    "ddots = r'\\ddots'\n",
    "cdots = r'\\cdots'\n",
    "vdots = r'\\vdots'\n",
    "pass_strs=[cdots, vdots,ddots,\"\"]\n",
    "type = \"v\"+\"matrix\"\n",
    "begin = r\"\\begin{\"+type+\"}\"\n",
    "end = r\"\\end{\"+type+\"}\"\n",
    "\n",
    "nrows=6\n",
    "ncols=6\n",
    "size = [nrows,ncols]\n",
    "# s = reduce(lambda x, y: x*y, size)\n",
    "s=nrows*ncols\n",
    "\n",
    "# 整形元素矩阵(arange()的参数是整数时,产生的时整形元素;参数是浮点数时,则产生浮点数矩阵)\n",
    "mat = np.arange(s).reshape(size)\n",
    "# 浮点型元素的矩阵(numpy默认为浮点型)\n",
    "# mat=np.ones(size)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 一般数字阵🎈\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\\begin{pmatrix}\n",
      "\t3&\t1&\t-1&\t2\t\\\\\n",
      "\t-5&\t1&\t3&\t-4\t\\\\\n",
      "\t2&\t0&\t1&\t-1\t\\\\\n",
      "\t1&\t-5&\t3&\t-3\t\\\\\n",
      "\\end{pmatrix}\n"
     ]
    }
   ],
   "source": [
    "# for i in range(len(mat)-1):\n",
    "#     mat[i+1,i]+=1\n",
    "# mat[0,-1]=1\n",
    "# mat=mat.astype('U7')\n",
    "\n",
    "# dots_line=4-1\n",
    "# mat[:, dots_line] = cdots\n",
    "# new_parts=np.where(mat[dots_line] == cdots, \"\", vdots)\n",
    "# mat[dots_line]=new_parts\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "# 填充你的矩阵😊\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [],
   "source": [
    "# mat=np.array(\n",
    "#     [[1],\n",
    "#      [0],\n",
    "#      [0],\n",
    "#      [0]\n",
    "#      ]\n",
    "# )\n",
    "mat=np.array(\n",
    "    [[3,1,-1,2],\n",
    "     [-5,1,3,-4],\n",
    "     [2,0,1,-1],\n",
    "     [1,-5,3,-3]]\n",
    ")\n",
    "\n",
    "# mat=np.array(\n",
    "#     [\n",
    "#         [1,0,1],\n",
    "#         [1,1,0],\n",
    "#         [0,1,1],\n",
    "#         # [0,0,0]\n",
    "#     ]\n",
    "# )\n",
    "\n",
    "# mat = np.array([[]])\n",
    "integer = False\n",
    "integer = True\n",
    "# print(begin,end)# (str(begin),end)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\\begin{vmatrix}\n",
      "\t3&\t1&\t-1&\t2\t\\\\\n",
      "\t-5&\t1&\t3&\t-4\t\\\\\n",
      "\t2&\t0&\t1&\t-1\t\\\\\n",
      "\t1&\t-5&\t3&\t-3\t\\\\\n",
      "\\end{vmatrix}\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# 使用元素遍历的方案,打印矩阵(行列式的latex代码)\n",
    "\n",
    "def print_num_matrix(mat,integer=True):\n",
    "    print(begin)\n",
    "    for i in mat:\n",
    "        l = [\n",
    "        str(j) if not integer else str(int(float(j)) if j not in pass_strs else j)\n",
    "        for j in i\n",
    "    ]\n",
    "        line = \"&\\t\".join(l) + \"\\t\" + r\"\\\\\"\n",
    "        print(\"\\t\" + line)\n",
    "    print(end)\n",
    "\n",
    "print_num_matrix(mat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 3,  1, -1,  2],\n",
       "       [-5,  1,  3, -4],\n",
       "       [ 2,  0,  1, -1],\n",
       "       [ 1, -5,  3, -3]])"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "mat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  3, -1,  2],\n",
       "       [ 0, -8,  4, -6],\n",
       "       [ 0,  2,  1, -1],\n",
       "       [ 0, 16, -2,  7]])"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#交换某两列(0,1)\n",
    "mat[:,[0,1]]=mat[:,[1,0]]\n",
    "mat[1]-=mat[0]\n",
    "mat[3]+=5*mat[0]\n",
    "mat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\\begin{vmatrix}\n",
      "\t1&\t3&\t-1&\t2\t\\\\\n",
      "\t0&\t2&\t1&\t-1\t\\\\\n",
      "\t0&\t-8&\t4&\t-6\t\\\\\n",
      "\t0&\t16&\t-2&\t7\t\\\\\n",
      "\\end{vmatrix}\n"
     ]
    }
   ],
   "source": [
    "mat[[1,2]]=mat[[2,1]]\n",
    "print_num_matrix(mat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\\begin{vmatrix}\n",
      "\t1&\t3&\t-1&\t2\t\\\\\n",
      "\t0&\t2&\t1&\t-1\t\\\\\n",
      "\t0&\t0&\t8&\t-10\t\\\\\n",
      "\t0&\t0&\t-10&\t15\t\\\\\n",
      "\\end{vmatrix}\n"
     ]
    }
   ],
   "source": [
    "mat[2]+=4*mat[1]\n",
    "mat[3]-=8*mat[1]\n",
    "mat\n",
    "print_num_matrix(mat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\\begin{vmatrix}\n",
      "\t1&\t3&\t-1&\t2\t\\\\\n",
      "\t0&\t2&\t1&\t-1\t\\\\\n",
      "\t0&\t0&\t4&\t-5\t\\\\\n",
      "\t0&\t0&\t-2&\t3\t\\\\\n",
      "\\end{vmatrix}\n"
     ]
    }
   ],
   "source": [
    "mat=mat.astype('float64')\n",
    "mat[2]/=2\n",
    "mat[3]/=5\n",
    "print_num_matrix(mat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\\begin{vmatrix}\n",
      "\t1&\t3&\t-1&\t2\t\\\\\n",
      "\t0&\t2&\t1&\t-1\t\\\\\n",
      "\t0&\t0&\t4&\t-5\t\\\\\n",
      "\t0&\t0&\t0&\t0\t\\\\\n",
      "\\end{vmatrix}\n"
     ]
    }
   ],
   "source": [
    "mat[3]+=mat[2]/2\n",
    "print_num_matrix(mat)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 使用array-to-latex库\n",
    "- 介绍:[array-to-latex · PyPI](https://pypi.org/project/array-to-latex/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\\begin{bmatrix}\n",
      "  1.00 &  3.00 & -1.00 &  2.00\\\\\n",
      "  0.00 &  2.00 &  1.00 & -1.00\\\\\n",
      "  0.00 &  0.00 &  4.00 & -5.00\\\\\n",
      "  0.00 &  0.00 &  0.00 &  0.50\n",
      "\\end{bmatrix}\n"
     ]
    }
   ],
   "source": [
    "import array_to_latex as a2l\n",
    "a2l.to_ltx(mat)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 单位阵生成@对角阵\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "mat=np.identity(4)\n",
    "#是否转换为整型\n",
    "mat=mat.astype('int32')\n",
    "# ddots=\"*\"\n",
    "# dia=[1,2,3]\n",
    "#微调对角阵内容(还可以加行/列,不一定要是方阵)\n",
    "# dia = [1, ddots, 1, 'k', 1, ddots, 1]\n",
    "# dia = [1, ddots, 1, ddots, 1, ddots, 1]\n",
    "# dia_cnt = list(range(1, len(dia)+1))\n",
    "# print(np.diag(dia_cnt))\n",
    "# dia_width=list(map(lambda x: format(str(x), \"^8\"),dia))\n",
    "# mat=np.diag([1,ddots,1,'k',1,ddots,1])\n",
    "# mat=np.full((4,4),format(\"\",\"7\"))\n",
    "# mat = np.diag(dia)\n",
    "#插入一列\n",
    "# mat=np.insert(mat,3,[0,0,0],axis=1)\n",
    "#插入一行\n",
    "# mat=np.insert(mat,3,[0,0,0],axis=0)\n",
    "# mat[3-1, 5-1] = \"k\"\n",
    "# mat[3-1, 3-1] = cdots\n",
    "# mat[3-1, 3-1] = vdots\n",
    "mat = mat.astype('U7')\n",
    "mat[2,:]=vdots\n",
    "mat[:,2]=cdots\n",
    "mat[2,2]=ddots\n",
    "\n",
    "# print(mat, \"\\n\"*3)\n",
    "#\n",
    "# print(begin)\n",
    "# for i in mat:\n",
    "#     l = [str(j) if j!=0 else \" \" for j in i]\n",
    "#     line = \"&\\t\".join(l)+\"\\t\"+r\"\\\\\"\n",
    "#     print(\"\\t\"+line)\n",
    "# print(end)\n",
    "#\n",
    "print(begin)\n",
    "for i in mat:\n",
    "    show_zero=False\n",
    "    show_zero=True\n",
    "    l = [format(str(j), \"^2\") if j != 0 or show_zero else \" \" for j in i]\n",
    "    line = \"&\".join(l)+\"\\t\"*0+r\"\\\\\"\n",
    "    print(\"\\t\"+line)\n",
    "print(end)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 字母阵\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "char = r'\\alpha'\n",
    "m, n = 5,5\n",
    "size = [m, n]\n",
    "cell_gap = 8\n",
    "# mat=np.arange(size[0]**2).reshape(size)\n",
    "#但下标\n",
    "mat = np.array([[\"{}{}\".format(\"\", j) for j in range(1, n+1)]\n",
    "               for i in range(1, m+1)], dtype='U7')\n",
    "#双下标\n",
    "# mat = np.array([[\"{}{}\".format(i, j) for j in range(1, n+1)]\n",
    "#                for i in range(1, m+1)], dtype='U7')\n",
    "# 这里设定矩阵元素类型为长度不超过7的字符串(设定的过短会导致长字符被截断)\n",
    "\n",
    "dots_line=4-1\n",
    "mat[:, dots_line] = cdots\n",
    "##deprecated: # mat[3-1] = vdots\n",
    "new_parts=np.where(mat[dots_line] == cdots, \"\", vdots)\n",
    "mat[3-1]=new_parts\n",
    "#下标字母化\n",
    "mat[-1]='n'\n",
    "mat[:,-1]='n'\n",
    "\n",
    "print(mat, \"\\n\"*3)\n",
    "print(begin)\n",
    "for i in mat:\n",
    "    l = [format(char+'_{'+str(j)+'}', \"<\"+str(cell_gap)) if str(j)\n",
    "         not in pass_strs else format(str(j), \"<\"+str(cell_gap)) for j in i]\n",
    "    line = \"&\".join(l)+\"\\t\"+r\"\\\\\"\n",
    "    print(\"\\t\"+line)\n",
    "print(end)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 向量表示法@向量阵\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "char0='A'\n",
    "char = 'B'\n",
    "m, n = 4,4\n",
    "size = [m, n]\n",
    "cell_gap = 8\n",
    "# mat=np.arange(size[0]**2).reshape(size)\n",
    "mat = np.array([[\"{}\".format(i) for i in range(1, n+1)] for j in range(1,m+1)], dtype='U7')\n",
    "# 这里设定矩阵元素类型为长度不超过7的字符串(设定的过短会导致长字符被截断)\n",
    "mat[:, 3-1] = cdots\n",
    "# mat[3-1] = vdots\n",
    "# np.where(mat[3] == cdots, \"\", mat)\n",
    "# line_k=mat[2]\n",
    "# new_parts=np.where(mat[3-1] == cdots, \"\", vdots)\n",
    "# mat[3-1]=new_parts\n",
    "# print(mat,\"🎈\")\n",
    "\n",
    "# mat[2,0]=vdots\n",
    "mat2=np.array([[\"{}\".format(j) for i in range(1, n+1)] for j in range(1,m+1)], dtype='U7')\n",
    "print(mat,\"\\n\",\"-\"*20 ,\"\\n\"*2)\n",
    "print(begin)\n",
    "r=0\n",
    "for i in mat:\n",
    "    l = [format(char0+'_{'+ mat2[r, c]+'}'+char+'_{'+mat[r, c]+'}', \"<\"+str(cell_gap)) if str(mat[r,c])\n",
    "         not in [cdots, vdots,ddots] else format(mat[r, c], \"<\"+str(cell_gap)) for c in range(len(i))]\n",
    "    line = \"&\".join(l)+\"\\t\"+r\"\\\\\"\n",
    "    r+=1\n",
    "    print(\"\\t\"+line)\n",
    "print(end)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
